package com.jfs.strategy.message.impl;

import com.jfs.enums.MessageEnum;
import com.jfs.envent.EventPublisher;
import com.jfs.envent.entity.MessageDeviceCataLogEntity;
import com.jfs.observer.request.abstracts.AbstractProcessor;
import com.jfs.strategy.message.MessageStrategy;
import com.jfs.utils.XstreamUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.sip.RequestEvent;

/**
 * @Description 设备目录策略处理类
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/7/25
 */
@Component
@Slf4j
@AllArgsConstructor
public class MessageCataLogStrategy implements MessageStrategy {
    private final EventPublisher eventPublisher;

    @Override
    public boolean support(String content) {
        return content.contains(MessageEnum.MESSAGE_CATA_LOG.getCmd());
    }

    @Override
    public void processor(String content, AbstractProcessor abstractProcessor, RequestEvent requestEvent) {
        try {
            MessageDeviceCataLogEntity messageDeviceCataLogEntity = XstreamUtil.toBean(MessageDeviceCataLogEntity.class, content);
            messageDeviceCataLogEntity.setRequestEvent(requestEvent);
            messageDeviceCataLogEntity.setIp(abstractProcessor.getIpByRequest(requestEvent));
            messageDeviceCataLogEntity.setPort(abstractProcessor.getPortFromRequest(requestEvent));
            messageDeviceCataLogEntity.setTransport(abstractProcessor.getTransactionFormRequest(requestEvent));
            //发布查询设备目录事件
            eventPublisher.publish(messageDeviceCataLogEntity);
            //回复ok
            abstractProcessor.doSuccess(requestEvent);
        } catch (Exception e) {
            log.error("处理设备目录详情发生异常:{}", e.getMessage());
            e.printStackTrace();
        }
    }
}
